﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Data;
using System.Web.Script.Serialization;


namespace Ewts.ashx
{
    /// <summary>
    /// IsGoodCode 的摘要说明
    /// </summary>
    public class ProjectArea : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            string result = null;

            //根据归属地获取业务员列表
            if (context.Request["city"] != null && context.Request["city"].ToString().Trim() != "")
            {
                string city = "";
                int areaid = 0;
                int isnew = 0;
                city = context.Request["city"].ToString().Trim();

                if (context.Request["areaid"] != null && context.Request["areaid"].ToString().Trim() != "")
                {
                    areaid = int.Parse(context.Request["areaid"].ToString().Trim());
                }
                if (context.Request["isnew"] != null && context.Request["isnew"].ToString().Trim() != "")
                {
                    isnew = int.Parse(context.Request["isnew"].ToString().Trim());
                }
                result = GetSalemanByAdd(city, areaid, isnew);
            }
          
            context.Response.Write(result);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        public List<salemanModel> GetList(string city)
        {
            List<salemanModel> list = new List<salemanModel>();
            salemanModel model = null;
            DataTable dt = new DataTable();
            dt = new DataLib.Accounts.Data.User().getUserList("department = '市场部' ");
            if (dt != null && dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    model = new salemanModel();
                    if (dt.Rows[i]["UserName"] != null)
                    {
                        model.username = dt.Rows[i]["UserName"].ToString();
                    }
                    if (dt.Rows[i]["TrueName"] != null)
                    {
                        model.truename = dt.Rows[i]["TrueName"].ToString();
                    }
                    model.city = city;
                    list.Add(model);
                    model = null;
                }
            }
            return list;
        }

        public String GetSalemanByAdd(string citycode, int areaid, int isnew)
        {
             StringBuilder strhtml = new StringBuilder();


             string city = new DataLib.BLL.City().GetModelByCode(citycode,"01").cityname; ;
             DataLib.BLL.ServerPoint spbll = new DataLib.BLL.ServerPoint();
             DataLib.Model.ServerPoint spmodel  = spbll.GetPointID(areaid, city);
            //可查看业务员的checkboxlist
            strhtml.Append("<tr class='" + citycode + "'>");
            strhtml.Append("<td><label>" + city +"可查看业务员：</label></td>");
            strhtml.Append("<td class='choose' title='" + city + "'>");

            List<salemanModel> list = GetList(city);
            strhtml.Append("<table id=''>");
            int i;
            for (i = 0; i < list.Count;i++ )
            {
                if (i % 12 == 0 && i != 0)
                {
                    strhtml.Append("</tr>");
                }

                if (i % 12 == 0)
                {
                    strhtml.Append("<tr>");
                }

                strhtml.Append("<td>");
                strhtml.Append("<input alt='" + list[i].username + "' title='" + list[i].truename + "' ct='" + city + "' cc='" + citycode + "'");
                if (isnew==0 && spmodel != null && spmodel.LookSalesmanUserName.IndexOf(list[i].username) >= 0)
                {
                    strhtml.Append(" checked='checked' ");
                }
                strhtml.Append(" onclick='Addcjoview(this)' type='checkbox' name='cjoview_chk' />");
                strhtml.Append("<label>" + list[i].truename +"</label>");
                strhtml.Append("</td>");

                if (i == list.Count)
                {
                    if (i%12 != 0)
                    {
                        int m = i % 12;
                        for (int r = 0; r < m; r++)
                        {
                            strhtml.Append("<td></td>");
                        }
                    }
                    strhtml.Append("</tr");
                }
            }
            strhtml.Append("</table>");
            strhtml.Append("</td>");
            strhtml.Append("</tr>");

            //开CJO人员
            strhtml.Append("<tr class='" + citycode + "'>");
            strhtml.Append("<td><label>" + city + "开CJO人员：</label></td>");
            strhtml.Append("<td class='choose' title='" + city + "'>");
            strhtml.Append("<select id='cjocreate" + citycode + "' onchange=\"onchangeByname('cjocreate','" + citycode + "','cjocreate" + citycode + "')\"> ");
            strhtml.Append("<option");
            if (spmodel == null || isnew == 1)
            {
                strhtml.Append(" selected='selected' ");
            }
            strhtml.Append(" value=''>--请选择--</option>");
            for (i = 0; i < list.Count; i++)
            {
                strhtml.Append("<option value='" + list[i].username + "'");
                if (isnew==0 && spmodel != null && spmodel.CjopersonUserName.IndexOf(list[i].username) >= 0)
                    strhtml.Append(" selected='selected'");
                strhtml.Append(">" + list[i].truename + "</option>");
            }
            strhtml.Append("</select>");
            strhtml.Append("</td>");
            strhtml.Append("</tr>");

            //授权人员
            strhtml.Append("<tr class='" + citycode + "'>");
            strhtml.Append("<td><label>" + city + "授权人员：</label></td>");
            strhtml.Append("<td class='choose' title='" + city + "'>");
            strhtml.Append("<select id='cjoauth" + citycode + "' onchange=\"onchangeByname('cjoauth','" + citycode + "','cjoauth" + citycode + "')\"> ");
            strhtml.Append("<option");
            if (spmodel == null || isnew == 1)
            {
                strhtml.Append(" selected='selected' ");
            }
            strhtml.Append(" value=''>--请选择--</option>");
            for (i = 0; i < list.Count; i++)
            {
                strhtml.Append("<option value='" + list[i].username + "'");
                if (isnew==0  && spmodel != null && spmodel.PowerpersonUserName.IndexOf(list[i].username) >= 0)
                    strhtml.Append(" selected='selected' ");
                strhtml.Append(">" + list[i].truename + "</option>");
            }
            strhtml.Append("</select>");
            strhtml.Append("</td>");
            strhtml.Append("</tr>");

            //显示业务员
            strhtml.Append("<tr class='" + citycode + "'>");
            strhtml.Append("<td><label>" + city + "显示业务人员：</label></td>");
            strhtml.Append("<td class='choose' title='" + city + "'>");
            strhtml.Append("<select id='cjoshow" + citycode + "' onchange=\"onchangeByname('cjoshow','" + citycode + "','cjoshow" + citycode + "')\"> ");
            strhtml.Append("<option");
            if (spmodel == null || isnew == 1)
            {
                strhtml.Append(" selected='selected' ");
            }
            strhtml.Append(" value=''>--请选择--</option>");
            for (i = 0; i < list.Count; i++)
            {
                strhtml.Append("<option value='" + list[i].username + "'");
                if (isnew==0  && spmodel != null && spmodel.Cjoperson_show.IndexOf(list[i].username) >= 0)
                    strhtml.Append(" selected='selected' ");
                strhtml.Append(">" + list[i].truename + "</option>");
            }
            strhtml.Append("</select>");
            strhtml.Append("</td>");
            strhtml.Append("</tr>");


            return strhtml.ToString();

        }
    }

    [Serializable]
    public class salemanModel
    {
        private string _username;
        private string _truename;
        private string _city;
     
        /// <summary>
        /// 用户名
        /// </summary>
        public string username
        {
            get { return _username; }
            set { _username = value; }
        }

        /// <summary>
        /// 真实姓名
        /// </summary>
        public string truename
        {
            get { return _truename; }
            set { _truename = value; }
        }

        /// <summary>
        /// 城市
        /// </summary>
        public string city
        {
            get { return _city; }
            set { _city = value; }
        }

     
    }
}